#!/usr/bin/env bash
# This is the maintainence launcher for the snap, make necessary runtime
# environment changes to make the snap work here.  You may also insert
# security confinement/deprecation/obsoletion notice of the snap here.

# lint: The use of backticks in the printf messages is intended
# shellcheck disable=SC2016

set \
    -o errexit \
    -o errtrace \
    -o nounset \
    -o pipefail

#export IMPORTANT_ENVIRONMENT_VARIABLE=value

# Run the command, if fails, check and present security confinement
# warnings to the user
if ! "${@}" \
    && test "$(snapctl get disable-snap-confinement-warnings)" != true; then
    snap_confinement_warning_triggered=false
    if ! snapctl is-connected home; then
        snap_confinement_warning_triggered=true
        printf \
            "Warning: It appears that the %s snap isn't connected to the \`home\` interface, accessing files under your home directory will not be possible.\n\n" \
            "${SNAP_NAME}" \
            1>&2
        printf \
            "To fix this problem run the following command as root:\n\n    snap connect %s:home\n\n" \
            "${SNAP_NAME}" \
            1>&2
    fi

    if ! snapctl is-connected raw-usb; then
        snap_confinement_warning_triggered=true
        printf \
            "Warning: It appears that the %s snap isn't connected to the \`raw-usb\` interface, direct-access of USB devices will not be possible.\n\n" \
            "${SNAP_NAME}" \
            1>&2
        printf \
            "To fix this problem run the following command as root:\n\n    snap connect %s:raw-usb\n\n" \
            "${SNAP_NAME}" \
            1>&2
    fi

    if ! snapctl is-connected removable-media; then
        snap_confinement_warning_triggered=true
        printf \
            "Warning: It appears that the %s snap isn't connected to the \`removable-media\` interface, accessing files under /media and /mnt directory will not be possible.\n\n" \
            "${SNAP_NAME}" \
            1>&2
        printf \
            "To fix this problem run the following command as root:\n\n    snap connect %s:removable-media\n\n" \
            "${SNAP_NAME}" \
            1>&2
    fi

    if test "${snap_confinement_warning_triggered}" == true; then
        printf \
            "To disable these warnings, run the following command as root:\n\n    snap set %s disable-snap-confinement-warnings=true\n\n" \
            "${SNAP_NAME}" \
            1>&2
    fi
fi
